/**
 *	@file	config.c
 *	@brief	コンフィグ画面
 *	@date	06.01.30
 *	@author	Miyuki Iwasawa
 */
#include "common.h"
#include "gflib/touchpanel.h"
#include "system/procsys.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/brightness.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/window.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/fontproc.h"
#include "system/snd_tool.h"
#include "system/clact_util.h"
#include "system/clact_tool.h"
#include "system/render_oam.h"
#include "system/wipe.h"
#include "application/app_tool.h"
#include "msgdata/msg.naix"
#include "msgdata/msg_config.h"

#include "savedata/config.h"
#include "application/config_tool.h"
#include "communication/communication.h"
#include "data/clact_util/clact_header.naix"
#include "config_gra.naix"
#include "config_snd_def.h"

#define CONFIG_DECIDE_END	(1)
#define CONFIG_CANCEL_END	(2)

#define FONT_TALK_PAL	(12)
#define FONT_SYS_PAL	(13)

#define WINCLR_COL(col)	(((col)<<4)|(col))

#define CONFIG_BMPCHAR_BASE	(10)
//20
#define BMPL_TITLE_PX	(1)
#define BMPL_TITLE_PY	(0)
#define BMPL_TITLE_SX	(12)
#define BMPL_TITLE_SY	(3)
#define BMPL_TITLE_PAL	(FONT_SYS_PAL)
#define BMPL_TITLE_CGX	(CONFIG_BMPCHAR_BASE)
#define BMPL_TITLE_FRM	(GF_BGL_FRAME1_M)

//600
#define BMPL_MENU_PX	(1)
#define BMPL_MENU_PY	(3)
#define BMPL_MENU_SX	(30)
#define BMPL_MENU_SY	(18)
#define BMPL_MENU_PAL	(FONT_SYS_PAL)
#define BMPL_MENU_CGX	(BMPL_TITLE_CGX+BMPL_TITLE_SX*BMPL_TITLE_SY)
#define BMPL_MENU_FRM	(GF_BGL_FRAME1_M)

#define BMPL_MENU_END_PX	(22)
#define BMPL_MENU_END_PY	(21)
#define BMPL_MENU_END_SX	( 8)
#define BMPL_MENU_END_SY	( 3)
#define BMPL_MENU_END_CGX	(BMPL_MENU_CGX+BMPL_MENU_SX*BMPL_MENU_SY)

#define BMPL_MENU_CANCEL_PX		(14)
#define BMPL_MENU_CANCEL_PY		(21)
#define BMPL_MENU_CANCEL_SX		( 8)
#define BMPL_MENU_CANCEL_SY		( 3)
#define BMPL_MENU_CANCEL_CGX	(BMPL_MENU_END_CGX+BMPL_MENU_END_SX*BMPL_MENU_END_SY)


//112
#define BMPL_SUM_PX	(2)
#define BMPL_SUM_PY	(19)
#define BMPL_SUM_SX	(27)
#define BMPL_SUM_SY	(4)
#define BMPL_SUM_PAL	(FONT_TALK_PAL)
#define BMPL_SUM_CGX	(1)
#define BMPL_SUM_FRM	(GF_BGL_FRAME1_S)

#define BMPL_MENU_WIN_CGX	(BMPL_MENU_END_CGX+BMPL_MENU_END_SX*BMPL_MENU_END_SY)
#define BMPL_TALK_WIN_CGX	(1+BMPL_SUM_SX*BMPL_SUM_SY)
#define BMPL_MENU_WIN_PAL	(14)
#define BMPL_TALK_WIN_PAL	(15)

enum{
	WIN_TITLE,
	WIN_MENU,
	WIN_SUM,
	WIN_MENU_END,
	WIN_MENU_CANCEL,
	WIN_MAX
};

#define MENU_SEL_MAX	(20)
#define MENU_LINE_MAX	(7)

#define MENU_STRSIZ	(10*2)
#define SUM_STRSIZ	(20*2)

#define MENU_SEL_OFSX	(12*8+12)
#define MENU_SEL_OFSY	(5)
#define MENU_SEL_DSIZ	(48)
#define MENU_SEL_ASIZX	(48*8)
#define MENU_SEL_ASIZY	(24)
#define MENU_WIN_OFSY	(24)

// メニュー項目定義
enum{
 MENU_MSG_SPD=0,
 MENU_BTL_EFF,
 MENU_BTL_RULE,
 MENU_SOUND,
 MENU_BTN_MODE,
 MENU_WIN,
 MENU_MAX,
};

// ConfigProc_Main内シーケンス定義
enum{
	CONFIG_SEQ_INIT=0,
	CONFIG_SEQ_FADEIN_WAIT,
	CONFIG_SEQ_MAIN,
	CONFIG_SEQ_BUTTON_ANM,
	CONFIG_SEQ_FADEOUT_WAIT,
	CONFIG_SEQ_EXIT,
};

// セルアクターのリソースID
enum {
	CONFIG_CL_ID_CURSOR1=0,	// カーソル
	CONFIG_CL_ID_CURSOR3,	//
	CONFIG_CL_ID_CURSOR4,	//
	CONFIG_CL_ID_CURSOR5,	//

	CONFIG_OBJ_RESOURCE_MAX,
};

// 画面に表示するセルタクターの番号定義
enum{
	CONFIG_OBJ_SPEED=0,
	CONFIG_OBJ_ANIME,
	CONFIG_OBJ_RULE,
	CONFIG_OBJ_SOUND,
	CONFIG_OBJ_BUTTONMODE,
	CONFIG_OBJ_LEFT,
	CONFIG_OBJ_RIGHT,
	CONFIG_OBJ_END,
	CONFIG_OBJ_CANCEL,
	CONFIG_OBJ_MAX,
};


// コンフィグカーソルのBGスクロール指定用テーブル
static const int cursor_tbl[]={
	-8,-32,-56,-80,-104,-128, -156,
};

// --------------------------------------------
// 構造体定義
// --------------------------------------------
typedef struct _CONFIG_MENU_DAT{
	u16	msgid;
	u16	ofs;
}CONFIG_MENU_DAT;

typedef struct _CONFIG_MENU{
	u16	num;
	u16	sel;
	STRBUF	*dat[MENU_SEL_MAX];
}CONFIG_MENU;

typedef struct _CONFIG_MAIN_DAT
{
	int	heapID;
	int	seq;
	int	sub_seq;
	int	wipe_f;
	u32	end_f:2;
	u32	line:3;
	u32	side:16;
	u32	wincgx_f:1;
	u32	pad:10;

	GF_BGL_INI	*bgl;
	CONFIG_PARAM	param;
	CONFIG			*save;

	///メッセージリソース
	MSGDATA_MANAGER*	pMsg;

	void*	pScrBuf;
	NNSG2dScreenData*	pScr01;

	GF_BGL_BMPWIN	win[WIN_MAX];	///<BMPウィンドウデータ
	CONFIG_MENU		menu[MENU_LINE_MAX];


	// セルアクター関連
	CATS_SYS_PTR	csp;
	CATS_RES_PTR	crp;
	CLACT_WORK_PTR	awp[CONFIG_OBJ_MAX];
	CATS_ACT_PTR	cap[CONFIG_OBJ_MAX];

	// キータッチ状態
	int keytouch;

}CONFIG_MAIN_DAT;

//プロトタイプ宣言　ローカル
//================================================================
///オーバーレイプロセス
PROC_RESULT ConfigProc_Init( PROC *proc,int *seq);
PROC_RESULT ConfigProc_Main( PROC *proc,int *seq);
PROC_RESULT ConfigProc_End( PROC *proc,int *seq);

///VBlank関連
static void ConfigVBlank(void * work);
///VRAMバンク定義
static void ConfigVramBankSet(void);

//================================================================
///プロトタイプエリア
//================================================================
static int ConfigInitCommon(CONFIG_MAIN_DAT* wk);
static int ConfigReleaseCommon(CONFIG_MAIN_DAT* wk);

static void ConfigBGLInit(CONFIG_MAIN_DAT* wk);
static void ConfigBGLRelease(CONFIG_MAIN_DAT* wk);
static void Config2DGraInit(CONFIG_MAIN_DAT* wk);
static void Config2DGraRelease(CONFIG_MAIN_DAT* wk);
static void ConfigBmpWinInit(CONFIG_MAIN_DAT* wk);
static void ConfigBmpWinRelease(CONFIG_MAIN_DAT* wk);
static void ConfigBmpWinWriteFirst(CONFIG_MAIN_DAT* wk);
static void ConfigBmpWinWriteMenu(CONFIG_MAIN_DAT* wk,u16 idx);
static void ConfigMenuStrGet(CONFIG_MAIN_DAT* wk);
static void ConfigKeyIn(CONFIG_MAIN_DAT *wk, CONFIG_MENU *mp);
static void ConfigCursorSet( CONFIG_MAIN_DAT *wk, int line );
static void ConfigCellActFigSet( CONFIG_MAIN_DAT *wk );


static void ConfigCellActInit( CONFIG_MAIN_DAT * wk );
static void ConfigCellActRerease( CONFIG_MAIN_DAT * wk );
static void ConfigCellActSet( CONFIG_MAIN_DAT * wk );
//static void config_obj_test( CONFIG_MAIN_DAT *wk );
static void ConfigCursorOff( CONFIG_MAIN_DAT *wk );
static void ConfigKeyTouch(CONFIG_MAIN_DAT* wk);
static void SelectFigureMove( CONFIG_MAIN_DAT *wk, int line, CONFIG_MENU *mp, int move );
static void ConfigWindowTypeNamePrint(CONFIG_MAIN_DAT *wk, STRBUF *buf);

static BOOL ButtonAnmCheck( CONFIG_MAIN_DAT * wk );


//================================================================
///データ定義エリア
//================================================================

//スクリーンサイズ取得マクロ
#define SCWS_GET(p)	((p)->screenWidth/8)
#define SCHS_GET(p)	((p)->screenHeight/8)

//--------------------------------------------------
///プログラムエリア
//----------------------------------------------------------------
//==============================================================================
/**
 *	@brief	コンフィグ プロセス初期化
 *	@param	proc	プロセスデータ
 *	@param	seq		シーケンス
 *
 *	@return	処理状況
 */
//==============================================================================
PROC_RESULT ConfigProc_Init( PROC *proc,int *seq)
{
	CONFIG_MAIN_DAT *wk = NULL;
	CONFIG_PARAM * cp;

	cp = (CONFIG_PARAM*)PROC_GetParentWork(proc);

	//ワークエリア取得
	HeapStatePush();

	//ヒープ作成
	sys_CreateHeap(HEAPID_BASE_APP,HEAPID_CONFIG,0x30000);

	wk = PROC_AllocWork( proc,sizeof(CONFIG_MAIN_DAT),HEAPID_CONFIG);
	memset(wk,0,sizeof(CONFIG_MAIN_DAT));


	//パラメータ引継ぎ
	wk->param.msg_spd		= CONFIG_GetMsgSpeed(cp->sp);
	wk->param.btl_eff		= CONFIG_GetWazaEffectMode(cp->sp);
	wk->param.btl_rule		= CONFIG_GetBattleRule(cp->sp);
	wk->param.sound			= CONFIG_GetSoundMode(cp->sp);
	wk->param.input_mode	= CONFIG_GetInputMode(cp->sp);
	wk->param.win_type		= CONFIG_GetWindowType(cp->sp);
	wk->param.kt_status     = cp->kt_status;
	wk->param.sp			= cp->sp;

	wk->heapID = HEAPID_CONFIG;
	wk->save   = cp->sp;

	wk->keytouch            = KeyTouchStatus_CheckTouchOrKey(wk->param.kt_status);

	WIPE_SetBrightness( WIPE_DISP_MAIN, WIPE_FADE_BLACK );
	WIPE_SetBrightness( WIPE_DISP_SUB,  WIPE_FADE_BLACK );


	return PROC_RES_FINISH;
}

//==============================================================================
/**
 *	@brief	コンフィグ プロセス終了
 *	@param	proc	プロセスデータ
 *	@param	seq		シーケンス
 *
 *	@return	処理状況
 */
//==============================================================================
PROC_RESULT ConfigProc_End( PROC *proc,int *seq)
{
	CONFIG_MAIN_DAT* wk = PROC_GetWork(proc);

	//セーブパラメータ書き出し
	if(wk->end_f == CONFIG_DECIDE_END){
		wk->param.msg_spd = wk->menu[MENU_MSG_SPD].sel;
		wk->param.btl_eff = wk->menu[MENU_BTL_EFF].sel;
		wk->param.btl_rule = wk->menu[MENU_BTL_RULE].sel;
		wk->param.sound	 = wk->menu[MENU_SOUND].sel;
		wk->param.input_mode	 = wk->menu[MENU_BTN_MODE].sel;
		wk->param.win_type	 = wk->menu[MENU_WIN].sel;
		CONFIG_SetMsgSpeed(wk->save,wk->param.msg_spd);
		CONFIG_SetWazaEffectMode(wk->save,wk->param.btl_eff);
		CONFIG_SetBattleRule(wk->save,wk->param.btl_rule);
		CONFIG_SetSoundMode(wk->save,wk->param.sound);
		CONFIG_SetInputMode(wk->save,wk->param.input_mode);
		CONFIG_SetWindowType(wk->save,wk->param.win_type);
	}else if(wk->end_f == CONFIG_CANCEL_END){
		//サウンドステートを元に戻す
		Snd_SetMonoFlag(wk->param.sound);
		CONFIG_SetKeyConfig(wk->param.input_mode);
	}


	//ワークエリア解放
	PROC_FreeWork(proc);

	HeapStatePop();
	HeapStateCheck(wk->heapID);
	sys_DeleteHeap(wk->heapID);

	return PROC_RES_FINISH;
}


//==============================================================================
/**
 *	@brief	コンフィグ プロセスメイン
 *	@param	proc	プロセスデータ
 *	@param	seq		シーケンス
 *
 *	@return	処理状況
 */
//==============================================================================
PROC_RESULT ConfigProc_Main( PROC *proc,int *seq)
{
	CONFIG_MAIN_DAT* wk = PROC_GetWork(proc);

	switch(wk->seq){

	// 画面初期化
	case CONFIG_SEQ_INIT:
		if(!ConfigInitCommon(wk)){
			return PROC_RES_CONTINUE;
		}
		wk->wipe_f = 0;
		WIPE_SYS_Start(WIPE_PATTERN_WMS,
				WIPE_TYPE_FADEIN,
				WIPE_TYPE_FADEIN,0x0000,COMM_BRIGHTNESS_SYNC,1,wk->heapID
				);
		// 選択箇所を示すOAMの位置設定
		ConfigCellActFigSet(wk);

		CATS_Draw(wk->crp);
		break;

	// フェードイン待ち
	case CONFIG_SEQ_FADEIN_WAIT:
		CATS_Draw(wk->crp);
		if(!WIPE_SYS_EndCheck()){
			return PROC_RES_CONTINUE;
		}
		break;

	//メイン
	case CONFIG_SEQ_MAIN:
		// 終了フラグが立っている？
		if(wk->end_f){
/*
			wk->wipe_f = 0;
			WIPE_SYS_Start(WIPE_PATTERN_WMS,
					WIPE_TYPE_FADEOUT,
					WIPE_TYPE_FADEOUT,0x0000,COMM_BRIGHTNESS_SYNC,1,wk->heapID
					);
*/
			CATS_Draw(wk->crp);
			break;		// このbreak;で++wk->seqの行へ
		}

		// キータッチ制御
		ConfigKeyTouch(wk);
		CATS_Draw(wk->crp);

		return PROC_RES_CONTINUE;

	// ボタンアニメ
	case CONFIG_SEQ_BUTTON_ANM:
		CATS_Draw(wk->crp);
		if( ButtonAnmCheck( wk ) == FALSE ){
			wk->wipe_f = 0;
			WIPE_SYS_Start(WIPE_PATTERN_WMS,
					WIPE_TYPE_FADEOUT,
					WIPE_TYPE_FADEOUT,0x0000,COMM_BRIGHTNESS_SYNC,1,wk->heapID
					);
			break;
		}
		return PROC_RES_CONTINUE;

	// フェードアウト待ち
	case CONFIG_SEQ_FADEOUT_WAIT:
		CATS_Draw(wk->crp);
		if(!WIPE_SYS_EndCheck()){
			return PROC_RES_CONTINUE;
		}
		break;

	//終了処理
	case CONFIG_SEQ_EXIT:
		if(!ConfigReleaseCommon(wk)){
			return PROC_RES_CONTINUE;
		}
		return PROC_RES_FINISH;
	}
	++wk->seq;
	return PROC_RES_CONTINUE;
}

//------------------------------------------------------------------
/**
 *	@brief	コンフィグ画面VramBankセット
 *
 * @param   none
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void ConfigVramBankSet(void)
{
	GF_BGL_DISPVRAM vramSetTable = {
		GX_VRAM_BG_128_A,				// メイン2DエンジンのBG
		GX_VRAM_BGEXTPLTT_NONE,			// メイン2DエンジンのBG拡張パレット
		GX_VRAM_SUB_BG_128_C,			// サブ2DエンジンのBG
		GX_VRAM_SUB_BGEXTPLTT_NONE,		// サブ2DエンジンのBG拡張パレット
		GX_VRAM_OBJ_16_G,				// メイン2DエンジンのOBJ
		GX_VRAM_OBJEXTPLTT_NONE,		// メイン2DエンジンのOBJ拡張パレット
		GX_VRAM_SUB_OBJ_16_I,			// サブ2DエンジンのOBJ
		GX_VRAM_SUB_OBJEXTPLTT_NONE,	// サブ2DエンジンのOBJ拡張パレット
		GX_VRAM_TEX_NONE,				// テクスチャイメージスロット
		GX_VRAM_TEXPLTT_NONE			// テクスチャパレットスロット
	};

	GF_Disp_SetBank( &vramSetTable );
}

//------------------------------------------------------------------
/**
 *	@brief	コンフィグ画面VBlank
 *
 * @param   work
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void ConfigVBlank(void * work)
{
	CONFIG_MAIN_DAT* wk = work;

	//会話ウィンドウキャラクタの再転送
	if(wk->wincgx_f){
		TalkWinGraphicSet(wk->bgl,BMPL_SUM_FRM,
			BMPL_TALK_WIN_CGX, BMPL_TALK_WIN_PAL, wk->menu[MENU_WIN].sel, wk->heapID);

		wk->wincgx_f = 0;
	}
	CATS_RenderOamTrans();
	NNS_GfdDoVramTransfer();	//VRam転送マネージャ実行
	GF_BGL_VBlankFunc( wk->bgl );
	OS_SetIrqCheckFlag( OS_IE_V_BLANK);
}

//------------------------------------------------------------------
/**
 *	@brief	各種初期化処理
 *
 * @param   wk		CONFIG_MAIN_DAT
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int ConfigInitCommon(CONFIG_MAIN_DAT *wk)
{
	switch(wk->sub_seq){
	case 0:
		sys_VBlankFuncChange(NULL,NULL);
		sys_HBlankIntrStop();

		GF_Disp_GX_VisibleControlInit();
		GF_Disp_GXS_VisibleControlInit();
		GX_SetVisiblePlane(0);
		GXS_SetVisiblePlane(0);

		//Bankセット
		ConfigVramBankSet();

		// 上下画面入れ替え
		GX_SetDispSelect(GX_DISP_SELECT_SUB_MAIN);

		WIPE_ResetWndMask(WIPE_DISP_MAIN);
		WIPE_ResetWndMask(WIPE_DISP_SUB);

		//BGLセット
		ConfigBGLInit(wk);

		// セルアクター初期化
		ConfigCellActInit(wk);
		break;
	case 1:
		//2Dリソース取得
		Config2DGraInit(wk);

		//メッセージリソース取得
		wk->pMsg = MSGMAN_Create(MSGMAN_TYPE_DIRECT,ARC_MSG,
				NARC_msg_config_dat,wk->heapID);
		ConfigMenuStrGet(wk);
		break;
	case 2:
		ConfigBmpWinInit(wk);
		ConfigBmpWinWriteFirst(wk);
		//通信アイコン用にOBJ面ON
		initVramTransferManagerHeap(32,wk->heapID);
		GF_Disp_GX_VisibleControl(GX_PLANEMASK_OBJ,VISIBLE_ON);


		// 受信強度アイコンを通信接続中なら表示するし、
		// ユニオンのように通信回路は動作しているが接続はしていない状態なら出さない
		// Change by Mori 2006/07/19
		WirelessIconEasyUnion();
		ConfigCellActSet( wk );

		sys_VBlankFuncChange(ConfigVBlank,wk);
		wk->sub_seq = 0;

		// 呼び出されたときがタッチかキーかでカーソルのON/OFFを決める
//		if(wk->keytouch){
//			GF_BGL_VisibleSet( GF_BGL_FRAME0_M, 0 );
//		}else{
			GF_BGL_VisibleSet( GF_BGL_FRAME0_M, 1 );
//		}

		return 1;

	}
	++wk->sub_seq;
	return 0;
}

//------------------------------------------------------------------
/**
 *	@brief	各種終了処理
 *
 * @param   wk		CONFIG_MAIN_DAT
 *
 * @retval  int		1で終了
 */
//------------------------------------------------------------------
static int ConfigReleaseCommon(CONFIG_MAIN_DAT *wk)
{
	int i = 0,j = 0;

	switch(wk->sub_seq){
	case 0:
		DellVramTransferManager();
		//Bmpウィンドウ破棄
		ConfigBmpWinRelease(wk);
		//メッセージリソース解放
		for(i = 0;i < MENU_LINE_MAX-1;i++){
			for(j = 0;j < wk->menu[i].num;j++){
				STRBUF_Delete(wk->menu[i].dat[j]);
			}
		}
		MSGMAN_Delete(wk->pMsg);
		//2Dリソース取得
		Config2DGraRelease(wk);
		ConfigBGLRelease(wk);
		ConfigCellActRerease( wk );

		break;
	case 1:
		sys_VBlankFuncChange(NULL,NULL);
		sys_HBlankIntrStop();

		GF_Disp_GX_VisibleControlInit();
		GF_Disp_GXS_VisibleControlInit();
		GX_SetVisiblePlane(0);
		GXS_SetVisiblePlane(0);
		wk->sub_seq = 0;
		return 1;
	}
	++wk->sub_seq;
	return 0;
}

//------------------------------------------------------------------
/**
 *	@brief	コンフィグ画面BGLシステム初期化
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void ConfigBGLInit(CONFIG_MAIN_DAT* wk)
{
	int i;

	wk->bgl = GF_BGL_BglIniAlloc(wk->heapID);

	{	//BG SYSTEM
		GF_BGL_SYS_HEADER BGsys_data = {
			GX_DISPMODE_GRAPHICS,GX_BGMODE_0,GX_BGMODE_0,GX_BG0_AS_2D
		};
		GF_BGL_InitBG(&BGsys_data);
	}

	{
	GF_BGL_BGCNT_HEADER TextBgCntDat[] = {
		{	//MAIN BG0
			0,0,0x800,0,GF_BGL_SCRSIZ_256x256,GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xf800,GX_BG_CHARBASE_0x00000,GX_BG_EXTPLTT_01,
			0,0,0,FALSE
		},
		{	//MAIN BG1
			0,0,0x800,0,GF_BGL_SCRSIZ_256x256,GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xf000,GX_BG_CHARBASE_0x04000,GX_BG_EXTPLTT_01,
			1,0,0,FALSE

		},
		{	//MAIN BG2
			0,0,0x800,0,GF_BGL_SCRSIZ_256x256,GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xe800,GX_BG_CHARBASE_0x00000,GX_BG_EXTPLTT_01,
			2,0,0,FALSE

		},
		{	//SUB BG0
			0,0,0x800,0,GF_BGL_SCRSIZ_256x256,GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xf800,GX_BG_CHARBASE_0x00000,GX_BG_EXTPLTT_01,
			1,0,0,FALSE
		},
		{	//SUB BG1
			0,0,0x800,0,GF_BGL_SCRSIZ_256x256,GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xf000,GX_BG_CHARBASE_0x08000,GX_BG_EXTPLTT_01,
			0,0,0,FALSE
		},
	};
	for(i = 0;i < 5;i++){
		static const frame[] = {
			GF_BGL_FRAME0_M,GF_BGL_FRAME1_M,GF_BGL_FRAME2_M,GF_BGL_FRAME0_S,GF_BGL_FRAME1_S,
		};
		GF_BGL_BGControlSet(wk->bgl,frame[i],&(TextBgCntDat[i]),GF_BGL_MODE_TEXT);
		GF_BGL_ScrClear(wk->bgl,frame[i]);
	}
	}
	GF_BGL_ClearCharSet(GF_BGL_FRAME0_M,32,0,wk->heapID);
	GF_BGL_ClearCharSet(GF_BGL_FRAME1_M,32,0,wk->heapID);
	GF_BGL_ClearCharSet(GF_BGL_FRAME0_S,32,0,wk->heapID);
	GF_BGL_ClearCharSet(GF_BGL_FRAME1_S,32,0,wk->heapID);
}

//------------------------------------------------------------------
/**
 *	@brief	コンフィグ画面BGLシステム破棄
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void ConfigBGLRelease(CONFIG_MAIN_DAT* wk)
{
	GF_BGL_BGControlExit(wk->bgl,GF_BGL_FRAME1_S);
	GF_BGL_BGControlExit(wk->bgl,GF_BGL_FRAME0_S);
	GF_BGL_BGControlExit(wk->bgl,GF_BGL_FRAME2_M);
	GF_BGL_BGControlExit(wk->bgl,GF_BGL_FRAME1_M);
	GF_BGL_BGControlExit(wk->bgl,GF_BGL_FRAME0_M);

	sys_FreeMemoryEz(wk->bgl);
}

#define MAIN_BG_PALSIZE	( 2*32 )
#define SUB_BG_PALSIZE	( 2*32 )


//------------------------------------------------------------------
/**
 *	@brief	コンフィグ画面 2Dリソース取得
 *
 * @param   wk		CONFIG_MAIN_DAT
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void Config2DGraInit(CONFIG_MAIN_DAT* wk)
{
	void* tmp;
	u32	size;
	ARCHANDLE* handle;
	void* pSrc;
	NNSG2dCharacterData* pChar;
	NNSG2dPaletteData*	pPal;


#if 0
	handle = ArchiveDataHandleOpen(ARC_CONFIG_GRA,wk->heapID);

	//キャラクタ転送
	size = ArchiveDataSizeGet(ARC_CONFIG_GRA,NARC_config_gra_config01_ncgr);
	pSrc = sys_AllocMemoryLo(wk->heapID,size);
	ArchiveDataLoadByHandle(handle,NARC_config_gra_config01_ncgr,(void*)pSrc);

	NNS_G2dGetUnpackedCharacterData(pSrc,&pChar);
	GF_BGL_LoadCharacter(wk->bgl,GF_BGL_FRAME0_M,
			pChar->pRawData,pChar->szByte,0);
	GF_BGL_LoadCharacter(wk->bgl,GF_BGL_FRAME0_S,
			pChar->pRawData,pChar->szByte,0);
	sys_FreeMemoryEz(pSrc);

	//パレット転送
	size = ArchiveDataSizeGet(ARC_CONFIG_GRA,NARC_config_gra_config01_nclr);
	pSrc = sys_AllocMemoryLo(wk->heapID,size);
	ArchiveDataLoadByHandle(handle,NARC_config_gra_config01_nclr,(void*)pSrc);

	NNS_G2dGetUnpackedPaletteData(pSrc,&pPal);
	GF_BGL_PaletteSet(GF_BGL_FRAME0_M,pPal->pRawData,0x20*1,0);
	GF_BGL_PaletteSet(GF_BGL_FRAME0_S,pPal->pRawData,0x20*1,0);
	sys_FreeMemoryEz(pSrc);

	//スクリーン取得
	size = ArchiveDataSizeGet(ARC_CONFIG_GRA,NARC_config_gra_config01_nscr);
	wk->pScrBuf = sys_AllocMemory(wk->heapID,size);
	ArchiveDataLoadByHandle(handle,NARC_config_gra_config01_nscr,(void*)wk->pScrBuf);
	NNS_G2dGetUnpackedScreenData(wk->pScrBuf,&(wk->pScr01));

	ArchiveDataHandleClose( handle );
#endif

	// -----------------------------------------
	//サブ（上画面）
	ArcUtil_PalSet( ARC_CONFIG_GRA, NARC_config_gra_config_up_nclr, PALTYPE_SUB_BG, 0,
						SUB_BG_PALSIZE, wk->heapID );
	//背景キャラクター
	ArcUtil_BgCharSet(	ARC_CONFIG_GRA, NARC_config_gra_config_up_ncgr,
						wk->bgl, GF_BGL_FRAME0_S, 0, 0, 0, wk->heapID );

	// スクリーン
	ArcUtil_ScrnSet(	ARC_CONFIG_GRA, NARC_config_gra_config_up1_nscr,
						wk->bgl, GF_BGL_FRAME0_S, 0, 0, 0, wk->heapID );


	// -----------------------------------------
	//メイン(下画面）

	//パレット
	ArcUtil_PalSet( ARC_CONFIG_GRA, NARC_config_gra_config_dw_nclr, PALTYPE_MAIN_BG, 0,
						MAIN_BG_PALSIZE, wk->heapID );
	//背景キャラクター
	ArcUtil_BgCharSet(	ARC_CONFIG_GRA, NARC_config_gra_config_dw_ncgr,
						wk->bgl, GF_BGL_FRAME0_M, 0, 0, 0, wk->heapID );

	// スクリーン
	ArcUtil_ScrnSet(	ARC_CONFIG_GRA, NARC_config_gra_config_dw1_nscr,
						wk->bgl, GF_BGL_FRAME2_M, 0, 0, 0, wk->heapID );

	// ---------カーソル----------

	// スクリーン
	ArcUtil_ScrnSet(	ARC_CONFIG_GRA, NARC_config_gra_config_dw_sel_nscr,
						wk->bgl, GF_BGL_FRAME0_M, 0, 0, 0, wk->heapID );


	// -----------------------------------------

	// コンフィグカーソル初期位置設定
	GF_BGL_ScrollSet(wk->bgl,GF_BGL_FRAME0_M,GF_BGL_SCROLL_Y_SET, cursor_tbl[wk->line]);


}

//------------------------------------------------------------------
/**
 *	@brief	コンフィグ画面 2Dリソース解放
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void Config2DGraRelease(CONFIG_MAIN_DAT* wk)
{
//	sys_FreeMemoryEz(wk->pScrBuf);
}

//------------------------------------------------------------------
/**
 *	@brief	コンフィグ画面 BMPウィンドウ初期化
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void ConfigBmpWinInit(CONFIG_MAIN_DAT* wk)
{
	// 「せっていをかえる」BMPWIN確保
	GF_BGL_BmpWinAdd(
		wk->bgl, &wk->win[WIN_TITLE], BMPL_TITLE_FRM,
		BMPL_TITLE_PX, BMPL_TITLE_PY,
		BMPL_TITLE_SX, BMPL_TITLE_SY, BMPL_TITLE_PAL, BMPL_TITLE_CGX );

	// メニュー項目名（縦に６項目）BMPWIN確保
	GF_BGL_BmpWinAdd(
		wk->bgl, &wk->win[WIN_MENU], BMPL_MENU_FRM,
		BMPL_MENU_PX, BMPL_MENU_PY,
		BMPL_MENU_SX, BMPL_MENU_SY, BMPL_MENU_PAL, BMPL_MENU_CGX );

	// 「けってい」BMPWIN確保
	GF_BGL_BmpWinAdd(
		wk->bgl, &wk->win[WIN_MENU_END], BMPL_MENU_FRM,
		BMPL_MENU_END_PX, BMPL_MENU_END_PY,
		BMPL_MENU_END_SX, BMPL_MENU_END_SY, BMPL_MENU_PAL, BMPL_MENU_END_CGX );

	// 「ウインドウタイプ●●」BMPWIN確保
	GF_BGL_BmpWinAdd(
		wk->bgl, &wk->win[WIN_SUM], BMPL_SUM_FRM,
		BMPL_SUM_PX, BMPL_SUM_PY,
		BMPL_SUM_SX, BMPL_SUM_SY, BMPL_SUM_PAL, BMPL_SUM_CGX );

	// 「やめる」BMPWIN確保
	GF_BGL_BmpWinAdd(
		wk->bgl, &wk->win[WIN_MENU_CANCEL], BMPL_MENU_FRM,
		BMPL_MENU_CANCEL_PX, BMPL_MENU_CANCEL_PY,
		BMPL_MENU_CANCEL_SX, BMPL_MENU_CANCEL_SY, BMPL_MENU_PAL, BMPL_MENU_CANCEL_CGX );


	//ウィンドウグラフィックセット
//	MenuWinGraphicSet(wk->bgl, BMPL_MENU_FRM,
//			BMPL_MENU_WIN_CGX, BMPL_MENU_WIN_PAL, MENU_TYPE_SYSTEM, wk->heapID);

	TalkWinGraphicSet(wk->bgl,BMPL_SUM_FRM,
			BMPL_TALK_WIN_CGX, BMPL_TALK_WIN_PAL, wk->param.win_type, wk->heapID);

	//フォント用パレットセット
	SystemFontPaletteLoad(PALTYPE_MAIN_BG,FONT_SYS_PAL*32,wk->heapID);
	SystemFontPaletteLoad(PALTYPE_SUB_BG,FONT_SYS_PAL*32,wk->heapID);
	TalkFontPaletteLoad(PALTYPE_MAIN_BG,FONT_TALK_PAL*32,wk->heapID);
	TalkFontPaletteLoad(PALTYPE_SUB_BG,FONT_TALK_PAL*32,wk->heapID);


	//BMPウィンドウクリア
	GF_BGL_BmpWinDataFill(&(wk->win[WIN_TITLE]),WINCLR_COL(FBMP_COL_NULL));
	GF_BGL_BmpWinDataFill(&(wk->win[WIN_MENU]),WINCLR_COL(FBMP_COL_NULL));
	GF_BGL_BmpWinDataFill(&(wk->win[WIN_MENU_END]),WINCLR_COL(FBMP_COL_NULL));
	GF_BGL_BmpWinDataFill(&(wk->win[WIN_MENU_CANCEL]),WINCLR_COL(FBMP_COL_NULL));
	GF_BGL_BmpWinDataFill(&(wk->win[WIN_SUM]),WINCLR_COL(FBMP_COL_WHITE));
	GF_BGL_BmpWinClearScrn(&(wk->win[WIN_SUM]));
	GF_BGL_BmpWinClearScrn(&(wk->win[WIN_MENU]));
	GF_BGL_BmpWinClearScrn(&(wk->win[WIN_TITLE]));

	//ウィンドウ描画
//	BmpMenuWinWrite( &wk->win[WIN_MENU], WINDOW_TRANS_OFF, BMPL_MENU_WIN_CGX, BMPL_MENU_WIN_PAL );
	BmpTalkWinWrite( &wk->win[WIN_SUM], WINDOW_TRANS_OFF, BMPL_TALK_WIN_CGX, BMPL_TALK_WIN_PAL);
}

//------------------------------------------------------------------
/**
 *	@brief	コンフィグ画面 BMPウィンドウ解放
 *
 * @param   wk		CONFIG_MAIN_DAT
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void ConfigBmpWinRelease(CONFIG_MAIN_DAT* wk)
{
	u16	i;

	//ウィンドウクリア
	BmpMenuWinClear(&(wk->win[WIN_MENU]),WINDOW_TRANS_ON);
	BmpTalkWinClear(&(wk->win[WIN_SUM]),WINDOW_TRANS_ON);

	for(i = 0;i < WIN_MAX;i++){
		GF_BGL_BmpWinOff(&(wk->win[i]));
		GF_BGL_BmpWinDataFill(&(wk->win[i]),0);
		GF_BGL_BmpWinClearScrn(&(wk->win[i]));
		GF_BGL_BmpWinDel(&wk->win[i]);
	}
}

//------------------------------------------------------------------
/**
 * @brief   上画面の「ウインドウタイプ●」を描画する
 *
 * @param   wk		CONFIG_MAIN_DAT0
 * @param   buf		STRBUF(確保済みの領域のポインタを渡す）
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void ConfigWindowTypeNamePrint(CONFIG_MAIN_DAT *wk, STRBUF *buf)
{
	GF_PRINTCOLOR wcol = GF_PRINTCOLOR_MAKE(FBMP_COL_BLACK,FBMP_COL_BLK_SDW,FBMP_COL_WHITE);


	MSGMAN_GetString(wk->pMsg,mes_config_wtype01+wk->menu[MENU_WIN].sel,buf);

	GF_BGL_BmpWinDataFill( &wk->win[WIN_SUM], FBMP_COL_WHITE | (FBMP_COL_WHITE<<4) );

	GF_STR_PrintColor(	&wk->win[WIN_SUM],FONT_TALK,buf,
						4,0,
						MSG_ALLPUT,wcol,NULL );

}

//------------------------------------------------------------------
/**
 *	@brief	コンフィグ画面 BMPウィンドウ初期描画
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void ConfigBmpWinWriteFirst(CONFIG_MAIN_DAT* wk)
{
	u32	siz,ofs;
	u16	i;
	GF_PRINTCOLOR fcol,rcol,tcol,wcol;
	STRBUF	*buf;
	STRBUF	*sum;

	tcol = GF_PRINTCOLOR_MAKE(FBMP_COL_WHITE,FBMP_COL_BLK_SDW,FBMP_COL_NULL);
	fcol = GF_PRINTCOLOR_MAKE(FBMP_COL_WHITE,FBMP_COL_BLK_SDW,FBMP_COL_NULL);
	rcol = GF_PRINTCOLOR_MAKE(FBMP_COL_BLACK,FBMP_COL_BLK_SDW,FBMP_COL_NULL);
	wcol = GF_PRINTCOLOR_MAKE(FBMP_COL_BLACK,FBMP_COL_BLK_SDW,FBMP_COL_NULL);


	buf = STRBUF_Create(SUM_STRSIZ,wk->heapID);

	//せっていをかえる
	MSGMAN_GetString(wk->pMsg,mes_config_title,buf);

	ofs = 2;
	GF_STR_PrintColor(	&wk->win[WIN_TITLE],FONT_SYSTEM,buf,
						ofs,5,
						MSG_ALLPUT,tcol,NULL );

	//「ウインドウタイプ●●」
	STRBUF_Clear(buf);
	ConfigWindowTypeNamePrint(wk, buf);

	//メニュー名描画
	ofs = 4;
	for(i = 0;i < MENU_LINE_MAX-1;i++){
		STRBUF_Clear(buf);
		MSGMAN_GetString(wk->pMsg,mes_config_menu01+i,buf);

		GF_STR_PrintColor(	&wk->win[WIN_MENU],FONT_SYSTEM,buf,
						ofs,MENU_SEL_ASIZY*i+MENU_SEL_OFSY,
						MSG_NO_PUT,fcol,NULL );
	}
	// 「やめる」描画
	STRBUF_Clear(buf);
	MSGMAN_GetString(wk->pMsg,mes_config_menu08,buf);
	GF_STR_PrintColor(	&wk->win[WIN_MENU_END],FONT_SYSTEM,buf,
					12,6,
					MSG_NO_PUT,fcol,NULL );

	// 「けってい」描画
	STRBUF_Clear(buf);
	MSGMAN_GetString(wk->pMsg,mes_config_menu07,buf);
	GF_STR_PrintColor(	&wk->win[WIN_MENU_CANCEL],FONT_SYSTEM,buf,
					4,6,
					MSG_NO_PUT,fcol,NULL );


	// メニュー項目描画
	for(i = 0;i < MENU_LINE_MAX;i++){
		ConfigBmpWinWriteMenu(wk,i);
	}

	// VRAM転送
	GF_BGL_BmpWinOn(&wk->win[WIN_TITLE]);
	GF_BGL_BmpWinOn(&wk->win[WIN_MENU]);
	GF_BGL_BmpWinOn(&wk->win[WIN_MENU_END]);
	GF_BGL_BmpWinOn(&wk->win[WIN_MENU_CANCEL]);
	GF_BGL_BmpWinOn(&wk->win[WIN_SUM]);

	STRBUF_Delete(buf);
}

//------------------------------------------------------------------
/**
 *	@brief	コンフィグ画面 セレクトメニュー文字列取得
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void ConfigMenuStrGet(CONFIG_MAIN_DAT* wk)
{
	u16	i,j,ct;
	static const selSiz[MENU_LINE_MAX] = {
		MSGSPEED_MAX,WAZAEFF_MODE_MAX,BATTLERULE_MAX,SOUNDMODE_MAX,INPUTMODE_MAX,WINTYPE_MAX, 2
	};

	for(i = 0,ct = 0;i < MENU_LINE_MAX-1;i++){
		wk->menu[i].num = selSiz[i];
		for(j = 0;j < selSiz[i];j++){
			wk->menu[i].dat[j] = MSGMAN_AllocString(wk->pMsg,mes_config_sm01_00+ct++);
		}
	}
	//現在の選択ナンバー取得
	wk->menu[MENU_MSG_SPD].sel = wk->param.msg_spd;
	wk->menu[MENU_BTL_EFF].sel = wk->param.btl_eff;
	wk->menu[MENU_BTL_RULE].sel = wk->param.btl_rule;
	wk->menu[MENU_SOUND].sel = wk->param.sound;
	wk->menu[MENU_BTN_MODE].sel = wk->param.input_mode;
	wk->menu[MENU_WIN].sel = wk->param.win_type;
	wk->menu[MENU_MAX].sel = 0;
}

//------------------------------------------------------------------
/**
 *	@brief	コンフィグ画面 セレクトメニュー描画
 *
 * @param   wk		CONFIG_MAIN_DAT
 * @param   idx		描画するメニュー項目
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void ConfigBmpWinWriteMenu(CONFIG_MAIN_DAT* wk,u16 idx)
{
	GF_PRINTCOLOR fcol,rcol,scol;
	u16	i;
	u8	wait;
	s8	x_ofs = 0;
	static const s8 ofs[]    = { 0,0,0,0,0,-16,0 };
	static const s8 distance[] = { 0,0,32,32,32,0,0 };

	fcol = GF_PRINTCOLOR_MAKE(FBMP_COL_WHITE,FBMP_COL_BLK_SDW,FBMP_COL_NULL);
	rcol = GF_PRINTCOLOR_MAKE(FBMP_COL_BLACK,FBMP_COL_BLK_SDW,FBMP_COL_NULL);

	//一旦クリア
	GF_BGL_BmpWinFill( &(wk->win[WIN_MENU]),
		WINCLR_COL(FBMP_COL_NULL),
		MENU_SEL_OFSX+ofs[idx], MENU_SEL_OFSY+idx*MENU_SEL_ASIZY,MENU_SEL_ASIZX,MENU_SEL_ASIZY);

	//せっていをかえる
	switch(idx){
	case MENU_WIN:
		GF_STR_PrintColor(	&wk->win[WIN_MENU],FONT_SYSTEM,
						wk->menu[idx].dat[wk->menu[idx].sel],
						1*MENU_SEL_DSIZ+MENU_SEL_OFSX,
						MENU_SEL_ASIZY*idx+MENU_SEL_OFSY,
						MSG_NO_PUT,rcol,NULL );
		GF_BGL_BmpWinOn(&wk->win[WIN_MENU]);

		// 上画面の「ウインドウタイプ●」を描き直し
		{
			STRBUF *strbuf = STRBUF_Create(SUM_STRSIZ,wk->heapID);

			ConfigWindowTypeNamePrint(wk, strbuf);

			STRBUF_Delete(strbuf);
		}

		//ウィンドウキャラクタ再送フラグOn
		wk->wincgx_f = 1;
		return;
		break;

	//サウンドモードの場合、ステレオとモノラルを切り替える
	case MENU_SOUND:
		Snd_SetMonoFlag(wk->menu[idx].sel);
		break;

	// ボタンモード
	case MENU_BTN_MODE:
		CONFIG_SetKeyConfig(wk->menu[idx].sel);
		break;
	}


	x_ofs = 0;
	for(i = 0;i < wk->menu[idx].num;i++){
		if(i == wk->menu[idx].sel){
			scol = rcol;
		}else{
			scol = fcol;
		}
		if(i == wk->menu[idx].num-1){
			wait = MSG_NO_PUT;
		}else{
			wait = MSG_NO_PUT;
		}

		if(idx == MENU_BTN_MODE){
			GF_STR_PrintColor(	&wk->win[WIN_MENU],FONT_SYSTEM,wk->menu[idx].dat[i],
							MENU_SEL_OFSX-0+x_ofs+distance[idx]*i,
							MENU_SEL_ASIZY*idx+MENU_SEL_OFSY,
							wait,scol,NULL );

			x_ofs += FontProc_GetPrintStrWidth(FONT_SYSTEM,wk->menu[idx].dat[i],0) + 12;
		}else{
			GF_STR_PrintColor(	&wk->win[WIN_MENU],FONT_SYSTEM,wk->menu[idx].dat[i],
							i*MENU_SEL_DSIZ+MENU_SEL_OFSX+ofs[idx]+distance[idx]*i,
							MENU_SEL_ASIZY*idx+MENU_SEL_OFSY,
							wait,scol,NULL );
		}
	}
	GF_BGL_BmpWinOn(&wk->win[WIN_MENU]);
}

// -----------------------------------------------------------
// タッチパネル用定義
// -----------------------------------------------------------
static const RECT_HIT_TBL touch_tbl[] = {
	{ 26, 46,112,151,},		// おそい・ふつう・はやい
	{ 26, 46,160,200,},
	{ 26, 46,208,248,},

	{ 50, 70,112,151,},		// みる・みない
	{ 50, 70,160,200,},

	{ 74, 93,112,167,},		// いれかえ・かちぬき
	{ 74, 93,192,247,},

	{ 98,117,112,167,},		// ステレオ・モノラル
	{ 98,117,192,247,},

	{122,142,112,167,},		// ノーマル・L=A
	{122,142,192,247,},

	{146,166,110,150,},		// 左矢印・右矢印
	{146,166,208,253,},

	{172,191,128,180,},		// キャンセル

	{172,191,183,255,},		// けってい

	{TP_HIT_END,0,0,0},		// 終了データ
};


enum{
	CONFIG_TP_DELAY_SPEED=0,
	CONFIG_TP_NORMAL_SPEED,
	CONFIG_TP_RAPID_SPEED,
	CONFIG_TP_BATTLE_ON,
	CONFIG_TP_BATTLE_OFF,
	CONFIG_TP_EXCHANGE,
	CONFIG_TP_KEEP,
	CONFIG_TP_STEREO,
	CONFIG_TP_MONO,
	CONFIG_TP_BUTTON_NORMAL,
	CONFIG_TP_BUTTON_L_A,
	CONFIG_TP_LEFT,
	CONFIG_TP_RIGHT,
	CONFIG_TP_END,
	CONFIG_TP_CANCEL,
};


//==============================================================================
/**
 * @brief   選択項目の移動制限とカーソルアニメーション
 *
 * @param   wk		CONFIG_MAIN_DAT
 * @param   line	現在の選択列
 * @param   mp		設定項目ワーク
 * @param   move	移動方向( -1 or 1 )
 *
 * @retval  void
 */
//==============================================================================
static void SelectFigureMove( CONFIG_MAIN_DAT *wk, int line, CONFIG_MENU *mp, int move )
{
	// ウインドウフレームの選択の際は矢印にアニメさせる
	if(line==MENU_WIN){
		if(move==-1){
			CLACT_AnmChg( wk->awp[CONFIG_OBJ_LEFT], 1 );
		}else if(move==1){
			CLACT_AnmChg( wk->awp[CONFIG_OBJ_RIGHT], 1 );
		}
	}

	if(move>0){
		mp->sel = (mp->sel+move) % mp->num;
	}else if(move<0){
		mp->sel = (mp->sel+mp->num-1) % mp->num;
	}
}

//------------------------------------------------------------------
/**
 * @brief   キー制御
 *
 * @param   wk		CONFIG_MAIN_DAT
 * @param   mp		CONFIG_MENUのポインタ
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void ConfigKeyIn(CONFIG_MAIN_DAT *wk, CONFIG_MENU *mp)
{

	//決定ラインの時は左右キー入力無効
	if(wk->line != MENU_MAX){
		if(sys.trg & PAD_KEY_RIGHT){
//			mp->sel = (mp->sel+1)%mp->num;
			SelectFigureMove( wk, wk->line, mp, 1 );
			ConfigBmpWinWriteMenu(wk,wk->line);
			Snd_SePlay( SE_CONFIG_SELECT );
		}else if(sys.trg & PAD_KEY_LEFT){
//			mp->sel = (mp->sel+mp->num-1)%mp->num;
			SelectFigureMove( wk, wk->line, mp, -1 );
			ConfigBmpWinWriteMenu(wk,wk->line);
			Snd_SePlay( SE_CONFIG_SELECT );
		}

		// 選択箇所を示すOAMの位置設定
		ConfigCellActFigSet(wk);
	}else{
		// 一番下の列の制御
		// (後で左の「けってい」を増やしたので１になっている点に注意）
		if(sys.trg&PAD_KEY_LEFT){
			if(wk->menu[wk->line].sel==0){
				wk->menu[wk->line].sel=1;
				ConfigCursorSet( wk, wk->line );
				Snd_SePlay( SE_CONFIG_SELECT );
			}
		}else if(sys.trg&PAD_KEY_RIGHT){
			if(wk->menu[wk->line].sel==1){
				wk->menu[wk->line].sel=0;
				ConfigCursorSet( wk, wk->line );
				Snd_SePlay( SE_CONFIG_SELECT );
			}
		}
	}

	if(sys.trg & PAD_KEY_UP){
		wk->line = (wk->line+MENU_LINE_MAX-1)%MENU_LINE_MAX;
		ConfigCursorSet( wk, wk->line );
		Snd_SePlay( SE_CONFIG_SELECT );
	}else if(sys.trg & PAD_KEY_DOWN){
		wk->line = (wk->line+1)%MENU_LINE_MAX;
		ConfigCursorSet( wk, wk->line );
		Snd_SePlay( SE_CONFIG_SELECT );
	}else 	if((sys.trg & PAD_BUTTON_DECIDE) && (wk->line == (MENU_LINE_MAX-1))){
		// 「けってい」
		if(wk->menu[wk->line].sel==1){
			KeyTouchStatus_SetTouchOrKey( wk->param.kt_status, APP_END_KEY );
			Snd_SePlay( SE_CONFIG_SAVE );
			CLACT_AnmChg( wk->awp[CONFIG_OBJ_CANCEL], 3 );
			wk->end_f = CONFIG_DECIDE_END;
		}else{
		// 「やめる」
			KeyTouchStatus_SetTouchOrKey( wk->param.kt_status, APP_END_KEY );
			Snd_SePlay( SE_CONFIG_CANCEL );
			CLACT_AnmChg( wk->awp[CONFIG_OBJ_END], 3 );
			wk->end_f = CONFIG_CANCEL_END;
		}
	}else if(sys.trg & PAD_BUTTON_CANCEL){
		// Bボタンでキャンセル
		KeyTouchStatus_SetTouchOrKey( wk->param.kt_status, APP_END_KEY );
		Snd_SePlay( SE_CONFIG_CANCEL );
		if( wk->line == (MENU_LINE_MAX-1) && wk->menu[wk->line].sel == 0 ){
			CLACT_AnmChg( wk->awp[CONFIG_OBJ_END], 3 );
		}else{
			CLACT_AnmChg( wk->awp[CONFIG_OBJ_END], 2 );
		}
		wk->end_f = CONFIG_CANCEL_END;
	}


}


enum{
	SELECT_0=0,
	SELECT_1,
	SELECT_2,
	SELECT_MINUS,
	SELECT_PLUS,
	SELECT_DECIDE,
	SELECT_CANCEL,
};

// タッチ範囲の意味定義
static const touch_trg_sel[][2]={
	{ MENU_MSG_SPD,		SELECT_0 },
	{ MENU_MSG_SPD,		SELECT_1 },
	{ MENU_MSG_SPD,		SELECT_2 },

	{ MENU_BTL_EFF,		SELECT_0 },
	{ MENU_BTL_EFF,		SELECT_1 },

	{ MENU_BTL_RULE,	SELECT_0 },
	{ MENU_BTL_RULE,	SELECT_1 },

	{ MENU_SOUND,		SELECT_0 },
	{ MENU_SOUND,		SELECT_1 },

	{ MENU_BTN_MODE,	SELECT_0 },
	{ MENU_BTN_MODE,	SELECT_1 },

	{ MENU_WIN,			SELECT_MINUS },	// 項目をプラスする
	{ MENU_WIN,			SELECT_PLUS  },

	{ MENU_MAX,			SELECT_DECIDE },
	{ MENU_MAX,			SELECT_CANCEL },
};



//------------------------------------------------------------------
/**
 *	@brief	設定画面キー取得
 *
 * @param   wk		CONFIG_MAIN_DAT
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void ConfigKeyTouch(CONFIG_MAIN_DAT* wk)
{
	CONFIG_MENU *mp;


	// タッチ入力
	if(sys.tp_trg){
		int touch = GF_TP_RectHitTrg( touch_tbl );
		switch(touch){

		// けってい
		case CONFIG_TP_END:
			wk->line = touch_trg_sel[touch][0];
			mp = &(wk->menu[wk->line]);

			ConfigCellActFigSet(wk);
			ConfigCursorOff( wk );
			wk->end_f = CONFIG_DECIDE_END;
			Snd_SePlay( SE_CONFIG_SAVE );
			wk->keytouch=APP_KTST_TOUCH;
			KeyTouchStatus_SetTouchOrKey(wk->param.kt_status, APP_END_TOUCH );
			wk->menu[wk->line].sel = 1;
			ConfigCursorSet( wk, wk->line );
			CLACT_AnmChg( wk->awp[CONFIG_OBJ_CANCEL], 3 );
			break;

		case CONFIG_TP_CANCEL:
			wk->line = touch_trg_sel[touch][0];
			mp = &(wk->menu[wk->line]);

			ConfigCellActFigSet(wk);
			ConfigCursorOff( wk );
			wk->end_f = CONFIG_CANCEL_END;
			Snd_SePlay( SE_CONFIG_CANCEL );
			wk->keytouch=APP_KTST_TOUCH;
			KeyTouchStatus_SetTouchOrKey(wk->param.kt_status, APP_END_TOUCH );
			wk->menu[wk->line].sel = 0;
			ConfigCursorSet( wk, wk->line );
			CLACT_AnmChg( wk->awp[CONFIG_OBJ_END], 3 );
			break;

		case TP_HIT_NONE:
			break;

		// ほかのタッチ範囲の時
		default:
			// 矢印の時は数値の増減を。それ以外は設定変更を。
			wk->line = touch_trg_sel[touch][0];
			mp = &(wk->menu[wk->line]);

			if(touch_trg_sel[touch][1]==SELECT_MINUS){
				SelectFigureMove( wk, wk->line, mp, -1 );
			}else if(touch_trg_sel[touch][1]==SELECT_PLUS){
				SelectFigureMove( wk, wk->line, mp, 1 );
			}else{
				mp->sel = touch_trg_sel[touch][1];
			}

			// 変更した内容を画面に反映
			ConfigBmpWinWriteMenu(wk,wk->line);
			// 選択箇所を示すOAMの位置設定
			ConfigCursorSet( wk, wk->line );
			ConfigCellActFigSet(wk);
			ConfigCursorOff( wk );

			wk->keytouch=APP_KTST_TOUCH;
			Snd_SePlay( SE_CONFIG_SELECT );

		}

	// キー入力
	}else if(sys.trg){

		mp = &(wk->menu[wk->line]);

		// タッチ状態のときは
//		if(wk->keytouch==APP_KTST_TOUCH){
//			ConfigCursorSet( wk, wk->line );
//			// カーソルを表示させるだけ
//			wk->keytouch=APP_KTST_KEY;
//			Snd_SePlay( SE_CONFIG_SELECT );
//		}else{
			ConfigKeyIn( wk, mp );
//		}


	}
}


//------------------------------------------------------------------
/**
 * @brief   カーソルの位置毎に形状をかえる
 *
 * @param   wk		CONFIG_DEMO_DAT
 * @param   line	カーソルの位置
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void ConfigCursorSet( CONFIG_MAIN_DAT *wk, int line )
{
	// 「けってい」の時はBGを隠し、OAMで表示する
	if(line==MENU_MAX){
		GF_BGL_VisibleSet( GF_BGL_FRAME0_M, 0 );
		if(wk->menu[line].sel==0){
			CLACT_AnmChg( wk->awp[CONFIG_OBJ_END],    1 );
			CLACT_AnmChg( wk->awp[CONFIG_OBJ_CANCEL], 0 );
		}else{
			CLACT_AnmChg( wk->awp[CONFIG_OBJ_END],    0 );
			CLACT_AnmChg( wk->awp[CONFIG_OBJ_CANCEL], 1 );
		}
	}else{
		// 「けってい」以外の場合はBGを表示し、「けってい」のOAMは隠す
		GF_BGL_ScrollSet(wk->bgl,GF_BGL_FRAME0_M,GF_BGL_SCROLL_Y_SET, cursor_tbl[wk->line]);

		CLACT_AnmChg( wk->awp[CONFIG_OBJ_END],    0 );
		CLACT_AnmChg( wk->awp[CONFIG_OBJ_CANCEL], 0 );
		GF_BGL_VisibleSet( GF_BGL_FRAME0_M, 1 );

	}
}

static void ConfigCursorOff( CONFIG_MAIN_DAT *wk )
{
	if(wk->line==MENU_MAX){
		GF_BGL_VisibleSet( GF_BGL_FRAME0_M, 0 );
	}

}

//============================================================================================
//============================================================================================
//============================================================================================
//============================================================================================
//============================================================================================
//============================================================================================
//============================================================================================
//============================================================================================
//============================================================================================
//============================================================================================
//============================================================================================
//============================================================================================
//============================================================================================
//============================================================================================
//============================================================================================
// セルアクター関連
//============================================================================================
//	定数定義
//============================================================================================

// カーソル座標定義
#define CONFIG_CURSOR1_X1	( 112)
#define CONFIG_CURSOR1_X2	( 160 )
#define CONFIG_CURSOR1_X3	( 160+48 )
#define CONFIG_CURSOR1_Y1	( 24 )
#define CONFIG_CURSOR1_Y2	( 48 )

#define CONFIG_CURSOR2_X1	( 112)
#define CONFIG_CURSOR2_X2	( 192 )
#define CONFIG_CURSOR2_Y1	( 72 )
#define CONFIG_CURSOR2_Y2	( 96  )
#define CONFIG_CURSOR2_Y3	( 120 )

#define CONFIG_CURSOR3_X	( 115 )
#define CONFIG_CURSOR4_X	( 213 )
#define CONFIG_CURSOR3_Y	( 144 )

#define CONFIG_CURSOR5_X	( 189 )
#define CONFIG_CURSOR5_Y	( 170 )

#define CONFIG_CURSOR6_X	( 116 )
#define CONFIG_CURSOR6_Y	( 170 )


//============================================================================================
//============================================================================================


//============================================================================================
//============================================================================================
static const TCATS_OBJECT_ADD_PARAM	ActAddParam[] =
{

//	int no;				///< リソース内データの登録番号
//
//	s16 x;				///< [ X ] 座標
//	s16 y;				///< [ Y ] 座標
//	s16 z;				///< [ Z ] 座標
//
//	u16	anm;			///< アニメ番号
//	int pri;			///< 優先順位
//	int	pal;			///< パレット番号
//	int d_area;			///< 描画エリア
//

	{	// カーソル１−１
		CONFIG_CL_ID_CURSOR1, CONFIG_CURSOR1_X1, CONFIG_CURSOR1_Y1, 0,
		0, 1, 0, NNS_G2D_VRAM_TYPE_2DMAIN, 0, 0, 0, 0
	},
	{	// カーソル１−２
		CONFIG_CL_ID_CURSOR1, CONFIG_CURSOR1_X1, CONFIG_CURSOR1_Y2, 0,
		0, 1, 0, NNS_G2D_VRAM_TYPE_2DMAIN, 0, 0, 0, 0
	},
	{	// カーソル２−１
		CONFIG_CL_ID_CURSOR1, CONFIG_CURSOR2_X1, CONFIG_CURSOR2_Y1, 0,
		1, 1, 0, NNS_G2D_VRAM_TYPE_2DMAIN, 0, 0, 0, 0
	},
	{	// カーソル２−２
		CONFIG_CL_ID_CURSOR1, CONFIG_CURSOR2_X1, CONFIG_CURSOR2_Y2, 0,
		1, 1, 0, NNS_G2D_VRAM_TYPE_2DMAIN, 0, 0, 0, 0
	},
	{	// カーソル２−３
		CONFIG_CL_ID_CURSOR1, CONFIG_CURSOR2_X1, CONFIG_CURSOR2_Y3, 0,
		1, 1, 0, NNS_G2D_VRAM_TYPE_2DMAIN, 0, 0, 0, 0
	},
	{	// カーソル３
		CONFIG_CL_ID_CURSOR3, CONFIG_CURSOR3_X, CONFIG_CURSOR3_Y, 0,
		0, 1, 0, NNS_G2D_VRAM_TYPE_2DMAIN, 0, 0, 0, 0
	},
	{	// カーソル４
		CONFIG_CL_ID_CURSOR4, CONFIG_CURSOR4_X, CONFIG_CURSOR3_Y, 0,
		0, 1, 0, NNS_G2D_VRAM_TYPE_2DMAIN, 0, 0, 0, 0
	},
	{	// カーソル５「けってい」
		CONFIG_CL_ID_CURSOR5, CONFIG_CURSOR5_X, CONFIG_CURSOR5_Y, 0,
		0, 1, 1, NNS_G2D_VRAM_TYPE_2DMAIN, 0, 0, 0, 0
	},
	{	// カーソル６「やめる」
		CONFIG_CL_ID_CURSOR5, CONFIG_CURSOR6_X, CONFIG_CURSOR6_Y, 0,
		0, 1, 1, NNS_G2D_VRAM_TYPE_2DMAIN, 0, 0, 0, 0
	},
};



//---------------------------------------------------------------------------------------------
/**
 * セルアクターシステム初期化
 *
 * @param	wk		ワーク
 *
 * @return	none
 */
//---------------------------------------------------------------------------------------------
static void ConfigCellActInit( CONFIG_MAIN_DAT * wk )
{
	GF_Disp_GX_VisibleControl(  GX_PLANEMASK_OBJ, VISIBLE_ON );		// MAIN DISP OBJ ON
	GF_Disp_GXS_VisibleControl( GX_PLANEMASK_OBJ, VISIBLE_ON );		// SUB DISP OBJ ON

	wk->csp = CATS_AllocMemory( wk->heapID );
	wk->crp = CATS_ResourceCreate( wk->csp );

	{
		TCATS_OAM_INIT	coi = {
			0, 128,
			0, 32,
			0, 128,
			0, 32,
		};
		TCATS_CHAR_MANAGER_MAKE ccmm = {
			CONFIG_OBJ_MAX,
			1024,
			1024,
			GX_OBJVRAMMODE_CHAR_1D_32K,
			GX_OBJVRAMMODE_CHAR_1D_32K
		};
		CATS_SystemInit( wk->csp, &coi, &ccmm, 32 );
		CATS_ClactSetInit( wk->csp, wk->crp, CONFIG_OBJ_MAX );
	}

	{
		//TCATS_RES_ARC
		TCATS_RESOURCE_FILE_LIST list = {
			NARC_clact_header_config_chr_resdat,		// キャラクタ
			NARC_clact_header_config_pal_resdat,		// パレット
			NARC_clact_header_config_cell_resdat,		// セル
			NARC_clact_header_config_canm_resdat,		// セルアニメ
			TCATS_RESID_NULL,							// マルチセル
			TCATS_RESID_NULL,							// マルチセルアニメ
			NARC_clact_header_config_h_cldat			// ヘッダー
		};
		CATS_ResourceLoadBinary( wk->csp, wk->crp, &list );
	}

	// メイン画面とサブ画面のアクター用距離設定（少し離す）
	CLACT_U_SetSubSurfaceMatrix( CATS_EasyRenderGet(wk->csp), 0, 256*FX32_ONE );

}

//---------------------------------------------------------------------------------------------
/**
 * セルアクターシステム削除
 *
 * @param	wk		ワーク
 *
 * @return	none
 */
//---------------------------------------------------------------------------------------------
static void ConfigCellActRerease( CONFIG_MAIN_DAT * wk )
{
	CATS_ResourceDestructor( wk->csp, wk->crp );
	CATS_FreeMemory( wk->csp );
	wk->crp=NULL;
}


//---------------------------------------------------------------------------------------------
/**
 * セルアクター追加
 *
 * @param	wk		ワーク
 *
 */
//---------------------------------------------------------------------------------------------
static void ConfigCellActSet( CONFIG_MAIN_DAT * wk )
{
	u16	i;

	for( i=0; i<CONFIG_OBJ_MAX; i++ ){
		wk->awp[i] = CATS_ObjectAdd( wk->csp, wk->crp, &ActAddParam[i] );

		// プライオリティ設定
		CATS_ObjectBGPriSet(wk->awp[i], 2);

		// オートアニメフラグセット
		CLACT_SetAnmFlag(wk->awp[i], 1);
	}

	// 終了ボタンのOBJは隠す
	CLACT_SetDrawFlag( wk->awp[CONFIG_OBJ_END], 1 );


}

// 項目の下地座標テーブル
static const clact_fig_tbl[][3]={
	{ CONFIG_CURSOR1_X1, CONFIG_CURSOR1_X2,CONFIG_CURSOR1_X3 },
	{ CONFIG_CURSOR1_X1, CONFIG_CURSOR1_X2,CONFIG_CURSOR1_X3 },
	{ CONFIG_CURSOR2_X1, CONFIG_CURSOR2_X2, },
	{ CONFIG_CURSOR2_X1, CONFIG_CURSOR2_X2, },
	{ CONFIG_CURSOR2_X1, CONFIG_CURSOR2_X2, },
};


//------------------------------------------------------------------
/**
 * @brief   現在選択している項目の上にアクターが乗るようにする
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void ConfigCellActFigSet( CONFIG_MAIN_DAT *wk )
{
	int i;
	s16 wx,wy;

	for(i=0;i<MENU_MAX-1;i++){
		CATS_ObjectPosGet(wk->awp[i], &wx, &wy);
		wx = clact_fig_tbl[i][wk->menu[i].sel];
		CATS_ObjectPosSet(wk->awp[i], wx, wy);
	}
}


static BOOL ButtonAnmCheck( CONFIG_MAIN_DAT * wk )
{
	if( CLACT_AnmActiveCheck( wk->awp[CONFIG_OBJ_END]) == FALSE &&
		CLACT_AnmActiveCheck( wk->awp[CONFIG_OBJ_CANCEL]) == FALSE ){
		return FALSE;
	}
	return TRUE;
}



#if 0
static int debugobj=0;
//==============================================================================
/**
 * @brief   OBJの座標確認をするルーチン
 *
 * @param   wk
 *
 * @retval  none
 */
//==============================================================================
static void config_obj_test( CONFIG_MAIN_DAT *wk )
{
	s16 wx,wy;
	if(sys.cont & PAD_KEY_UP){

		CATS_ObjectPosGet(wk->awp[debugobj], &wx, &wy);
		wy--;
		CATS_ObjectPosSet(wk->awp[debugobj], wx,  wy);
		OS_Printf("obj x=%d, y=%d\n", wx,wy);

	}else if(sys.cont & PAD_KEY_DOWN){
		CATS_ObjectPosGet(wk->awp[debugobj], &wx, &wy);
		wy++;
		CATS_ObjectPosSet(wk->awp[debugobj], wx,  wy);
		OS_Printf("obj x=%d, y=%d\n", wx,wy);
	}else if(sys.cont & PAD_KEY_LEFT){
		CATS_ObjectPosGet(wk->awp[debugobj], &wx, &wy);
		wx--;
		CATS_ObjectPosSet(wk->awp[debugobj], wx,  wy);
		OS_Printf("obj x=%d, y=%d\n", wx,wy);
	}else if(sys.cont & PAD_KEY_RIGHT){
		CATS_ObjectPosGet(wk->awp[debugobj], &wx, &wy);
		wx++;
		CATS_ObjectPosSet(wk->awp[debugobj], wx,  wy);
		OS_Printf("obj x=%d, y=%d\n", wx,wy);
	}else if(sys.trg & PAD_BUTTON_Y){
		if(++debugobj>CONFIG_OBJ_MAX-1){
			debugobj=0;
		}
	}
}

#endif